//@ts-nocheck
import {TEXT, ELEMENT, CLASS_COMPONENT, FUNCTION_COMPONENT} from './constants';
import {ReactElement} from './vdom'
import {Component} from './component'

function createElement(type, config = {}, ...children /** ←和源码不一样*/) {
  // eslint-disable-next-line prefer-rest-params
  console.log('createElement arguments:', arguments);
  !config && (config = {}); // emmm 是类组件时 莫名其妙这里的config为空时 不会被赋初始值; emmm 这是因为config会被现实的传递为null,而null并不会被认为没有传递值, 故上面的参数默认值config={}就不会生效
  delete config.__source;
  delete config.__self;
  const {key, ref, ...props} = config;
  let $$typeof = null;
  if (typeof type === 'string') { // span div button
    $$typeof = ELEMENT; // 是一个原生DOM类型
  } else if (typeof type === 'function' && type.prototype.isReactComponent) {
    $$typeof = CLASS_COMPONENT;
  } else if (typeof type === 'function') {
    $$typeof = FUNCTION_COMPONENT;
  }
  props.children = children.map(item => {
    if (typeof item === 'object') {
      return item; // React.createElement('span',{color:'red'},'Hello')
    } else {
      return {$$typeof: TEXT, type: TEXT, content: item};
    }
  });

  return ReactElement($$typeof, type, key, ref, props);
}

export {
  Component
}

const React = {
  createElement,
  Component
};

export default React
